package com.asialjim.wechat.apiservice.executor;

import com.asialjim.wechat.apiservice.executor.handler.UTF8ResponseHandler;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.IOException;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: PostApiExecutor ]</i></h2><strong>
 * <p> simple post api executor
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/1   &nbsp;&nbsp; JDK 8
 */
public class PostApiExecutor extends BaseApiExecutor {
    public PostApiExecutor(HttpHost httpProxy, String domain,String tempFilePath) {
        super(httpProxy,domain,tempFilePath);
    }

    @Override
    protected <T> T doExecute(HttpRequestBase httpRequest, Class<T> resultClass, ContentType responseContentType) throws IOException {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        try {
            CloseableHttpResponse response = client.execute(httpRequest);
            log.info("\r\n\tHTTP CLIENT:[{}] EXECUTE",client);
            UTF8ResponseHandler<T> handler = new UTF8ResponseHandler<>(resultClass,responseContentType);
            T t = handler.handleResponse(response);
            log.info("\r\n\tHTTP POST RESULT:[{}]",t);
            return t;
        } finally {
            httpRequest.releaseConnection();
        }
    }
}